package com.draemoncode.weixindb.util;

import org.springframework.util.Assert;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * <p>HTTP 请求工具类</p>
 *
 * @author  yangz
 * @date    2016/10/27
 * @version latest
 */
public class HTTPUtil {


    /**
     * 判断移动端请求
     * @param request
     * @return
     */
    public static boolean isMobile(HttpServletRequest request){
        String requestHeader = request.getHeader("user-agent");
        /**
         * android : 所有android设备
         * mac os : iphone ipad
         * windows phone:Nokia等windows系统的手机
         */
        String[] deviceArray = new String[]{"android","mac os","windows phone"};
        if(requestHeader == null)
            return false;
        requestHeader = requestHeader.toLowerCase();
        for(int i=0;i<deviceArray.length;i++){
            if(requestHeader.indexOf(deviceArray[i])>0){
                return true;
            }
        }
        return false;
    }

    /**
     * 判断ajax请求
     * @param request
     * @return
     */
    public static boolean isAjax(HttpServletRequest request){
        String requestType = request.getHeader("X-Requested-With");
        String accept = request.getHeader("Accept");
        return requestType != null || accept.contains("application/json");
    }

    /**
     * 判断超文本协议请求
     * @param request
     * @return
     */
    public static boolean isTextHtml(HttpServletRequest request) {
        Assert.notNull(request, "request can not be null.");
        String accept = request.getHeader("ACCEPT");
        Boolean isTextHtml = accept != null && (accept.contains("text/html") || accept.contains("application/x-ms-application") || accept.contains("application/xhtml+xml"));
        if(isTextHtml){
            return isTextHtml;
        }

        //ie8浏览器判断是否是html请求
        String userAgent = request.getHeader("User-Agent");
        if(userAgent!=null && userAgent.contains("MSIE 8.0")){
            isTextHtml = request.getHeader(" X-Requested-With") == null;
        }
        return isTextHtml;
    }

    /**
     * 打印所有请求参数
     * @param request
     * @return
     */
    public static String showParamsString(HttpServletRequest request, String division) {
        Map<String, Object> map = new HashMap();
        Enumeration paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = (String) paramNames.nextElement();

            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() != 0) {
                    map.put(paramName, paramValue);
                }
            }
        }
        return CollectionUtil.mapToString(map, division);
    }

    public static String showHeaderString(HttpServletRequest request, String division) {
        StringBuilder sb = new StringBuilder();
        Map map = new HashMap();
        Enumeration paramNames = request.getHeaderNames();
        while (paramNames.hasMoreElements()) {
            String paramName = (String) paramNames.nextElement();
            String paramValues = request.getHeader(paramName);
            map.put(paramName, paramValues);
        }

        Set<Map.Entry<String, String>> set = map.entrySet();
        sb.append(division + "----" + division);
        for (Map.Entry entry : set) {
            sb.append(entry.getKey() + " : " + entry.getValue() + division);
        }
        sb.append("----");
        return sb.toString();
    }

    public static String showCookieString(HttpServletRequest request, String division) {
        StringBuilder sb = new StringBuilder();
        Map map = new HashMap();
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for (Cookie cookie : cookies) {
                map.put(cookie.getName(), " value: " + cookie.getValue() + " path: " + cookie.getPath()  + " domain: " + cookie.getDomain()  + " maxage: " + cookie.getMaxAge());
            }
        }
        Set<Map.Entry<String, String>> set = map.entrySet();
        sb.append(division + "----" + division);
        for (Map.Entry entry : set) {
            sb.append(entry.getKey() + " : " + entry.getValue() + division);
        }
        sb.append("----");
        return sb.toString();
    }

}
